package Servlet;

import Pojo.Course;
import Pojo.Student;
import Service.CourseService;
import Service.Courseimply;
import dto.StudentCourse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CourseServlet extends BaseServlet{
    private final CourseService courseService=new Courseimply();
    //查看查询选课表
    protected void selectCourseTable(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Course> courses = courseService.queryCourseTable();
        req.setAttribute("course",courses);
        req.getRequestDispatcher("CoureTable.jsp").forward(req,resp);
    }

    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Course course = courseService.selectOne(Integer.valueOf(id));
        req.setAttribute("course",course);
        req.getRequestDispatcher("student_Edit.jsp").forward(req,resp);
    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        Integer sid = (Integer)session.getAttribute("id");
        String cid = req.getParameter("id");
//        Course course = courseService.selectOne(Integer.valueOf(cid));
//        courseService.add(course);
        StudentCourse studentCourse = new StudentCourse(Integer.valueOf(cid),sid);
        courseService.add(studentCourse);
        resp.sendRedirect(req.getContextPath()+"/courseServlet?action=selectCourseTable");
    }
    //查看选课表
    protected void selectStudentCourseTable(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        Integer id = (Integer)session.getAttribute("id");
        List<Course> courses = courseService.queryStudentCourseTable(id);
        if (courses.size() == 0){
             req.getRequestDispatcher("queryNone.jsp").forward(req,resp);
        }else{

        req.setAttribute("studentcourse",courses);
        req.getRequestDispatcher("studentAndteacher_manager.jsp").forward(req,resp);
        }
    }

}
